package com.sn.shop.utils;

import com.github.pagehelper.PageInfo;
import com.sn.shop.enums.ResultStatus;
import com.sun.org.apache.bcel.internal.generic.RETURN;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @auther sn
 * @date 2018/2/8
 * @description 返回结果
 */
public class ReturnResult implements Serializable {

    private static final long serialVersionUID = 1L;

    private ResultStatus status;
    private String message;
    private Object data;


    public int getStatus() {
        return status.getCode();
    }

    public void setStatus(ResultStatus status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 成功
     * @return
     */
    public static ReturnResult success() {
        return returnResult(ResultStatus.OK, "success", null);
    }

    public static ReturnResult success(Object data) {
        return success("success", data);
    }

    public static ReturnResult success(String message) {
        return success(message, null);
    }

    public static ReturnResult success(String message, Object data) {
        return returnResult(ResultStatus.OK, message, data);
    }

    public static ReturnResult error() {
        return returnResult(ResultStatus.ERROR, "error", null);
    }

    public static ReturnResult error(ResultStatus status) {
        return error("error");
    }

    public static ReturnResult error(String message) {
        return error(message, null);
    }

    public static ReturnResult error(String message, Object data) {
        return returnResult(ResultStatus.ERROR, message, data);
    }

    public static ReturnResult error(ResultStatus status, String message) {
        return returnResult(status, message, null);
    }

    /**
     * 将分页插件的接管转换成想要的形势
     * @param page
     * @return
     */
    public static Map<String, Object> convert(PageInfo<?> page) {
        Map<String, Object> result = new HashMap<>();
        result.put("data", page.getList());
        result.put("total", page.getTotal());
        return result;
    }

    /**
     * 返回统配方法
     * @param status 状态
     * @param message 信息
     * @param data 数据
     * @return
     */
    public static ReturnResult returnResult(ResultStatus status, String message, Object data) {
        ReturnResult rs = new ReturnResult();
        rs.setStatus(status);
        rs.setMessage(message);
        rs.setData(data);
        return rs;
    }
}
